<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      size="small"
      @keyup.enter.native="getDataList()"
    >
       <el-form-item label="仓库">
        <el-select v-model="dataForm.wareId" placeholder="请选择仓库" clearable>
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select
          v-model="dataForm.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option label="新建" :value="0"></el-option>
          <el-option label="已分配" :value="1"></el-option>
          <el-option label="已领取" :value="2"></el-option>
          <el-option label="已完成" :value="3"></el-option>
          <el-option label="有异常" :value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="优先级">
        <el-select
          v-model="dataForm.priority"
          placeholder="请选择优先级"
          clearable
        >
          <el-option label="紧急" :value="1"></el-option>
          <el-option label="高" :value="2"></el-option>
          <el-option label="中" :value="3"></el-option>
          <el-option label="低" :value="4"></el-option>
          <el-option label="无关紧要" :value="5"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column type="index" align="center" label="编号" width="80" />
       <el-table-column
        prop="purchaseName"
        header-align="center"
        align="center"
        label="采购单名称"
      ></el-table-column>
      <el-table-column
        prop="assigneeName"
        header-align="center"
        align="center"
        label="采购人姓名"
      ></el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="联系方式"
      ></el-table-column>
      <el-table-column
        prop="priority"
        header-align="center"
        align="center"
        label="优先级"
      >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.priority == 1">紧急</el-tag>
          <el-tag type="danger" v-if="scope.row.priority == 2">高</el-tag>
          <el-tag type="success" v-if="scope.row.priority == 3">中</el-tag>
          <el-tag type="info" v-if="scope.row.priority == 4">低</el-tag>
          <el-tag type="info" v-if="scope.row.priority == 5">无关紧要</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0">新建</el-tag>
          <el-tag type="info" v-if="scope.row.status == 1">已分配</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 2">已领取</el-tag>
          <el-tag type="success" v-if="scope.row.status == 3">已完成</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 4">有异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="wareName"
        header-align="center"
        align="center"
        label="仓库名称"
      ></el-table-column>
      <el-table-column
        prop="amount"
        header-align="center"
        align="center"
        label="总金额"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建日期"
      ></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./purchase-add-or-update";
import { warePurchasePageList,wareInfoList, warePurchaseRemove } from "@/api/ware";
export default {
  data() {
    return {
      currentRow: {},
      dataForm: {
        status: "",
        priority:'',
        wareId:'',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
    this.getWares();
  },
  created() {},
  methods: {
    getWares() {
      wareInfoList().then((res) => {
        if (res && res.code === 0) {
          this.wareList = res.data;
        } else {
          this.wareList = [];
        }
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      warePurchasePageList({
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
        status: this.dataForm.status,
        priority: this.dataForm.priority,
        wareId: this.dataForm.wareId,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        warePurchaseRemove({ ids: ids }).then((res) => {
          if (res && res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
};
</script>
